const errorType = require("../constants/error-types")

const errHandler = (error, ctx) => {
  console.log(error)
  let status, message
  switch(error.message) {
    case errorType.USRENAME_OR_PASSWORD_IS_EMPTY:
      status = 400
      message = "用户名或者密码不能为空~"
      break
    case errorType.USRENAME_IS_EXISTS:
      status = 400
      message = "用户名不存在~"
      break
    case errorType.PASSWORD_IS_EXISTS:
      status = 400
      message = "密码错误~"
      break
    case errorType.UNAUTHRORIZATION:
      status = 401
      message = "无效的token~"
      break
    case errorType.UNPREMISSION:
      status = 401
      message = "无操作权限~"
      break
    default:
      status = 404
      message = "默认错误"
  }
  ctx.status = status
  ctx.body = message
}

module.exports = errHandler